﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Collections.ObjectModel;

namespace MetaTracker.ViewModel
{
    class StatisticsViewModel : TrackerBaseViewModel
    {
        #region Data and Properties

        public ObservableCollection<MetaTracker.Model.WeeklyStatistics> WeeklyStats
        {
            get
            {
                return tracker.WeeklyStats;
            }
            
        }

        public bool IsUsingFatSecret
        {
            get
            {
                return tracker.FoodIntakeService == ServiceEnum.FatSecret
                    || tracker.ExerciseService == ServiceEnum.FatSecret
                    || tracker.WeightService == ServiceEnum.FatSecret;
            }
        }
        public bool IsUsingFitbit
        {
            get
            {
                return tracker.FoodIntakeService == ServiceEnum.Fitbit
                    || tracker.ExerciseService == ServiceEnum.Fitbit
                    || tracker.WeightService == ServiceEnum.Fitbit;
            }
        }

        #endregion //Data and Properties

        #region Constructors

        internal StatisticsViewModel()
        {
            tracker.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(tracker_PropertyChanged);
        }
        #region Events
        void tracker_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            NotifyPropertyChanged(e.PropertyName);
            if (e.PropertyName == "FoodIntakeService" || e.PropertyName == "ExerciseService"
                || e.PropertyName == "WeightService")
            {
                NotifyPropertyChanged("IsUsingFatSecret");
                NotifyPropertyChanged("IsUsingFitbit");
            }
        }
        #endregion //Events

        #endregion // Constructors

        #region Commands
        public ICommand RefreshCommand
        {
            get
            {
                return Commands.GetOrCreateCommand(
                    () => RefreshCommand,
                    (parameter) => DoRefreshCommand(parameter),
                    () => true);
            }
        }
        private void DoRefreshCommand(object parameter)
        {
            BuildStatistics(true);
        }

        #endregion //Commands

        #region Methods

        internal void BuildStatistics(bool forceUpdates = false)
        {
            tracker.BuildStatistics(forceUpdates);
            NotifyPropertyChanged("WeeklyStats");
        }

        #endregion //Methods

        
    }
}
